﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace Test_2D
{
    public partial class ComLib : DockContent
    {
        public ComLib()
        {
            InitializeComponent();
            init();
        }
        //public delegate void list_Change(List<Component> comlist);
        private List<Component> comList;
        public List<Component> ComList
        {
            set 
            { 
                this.comList = value;
                refreshList();
            }
        }

        private ImageList imageList = new ImageList();
        //public event ComList_Delete Delete;

        private void init()
        {
            listView1.LargeImageList = imageList;
            imageList.ImageSize = new Size(50, 50);       //设定缩略图的大小com1.width, com1.height
        }

        private void refreshList()
        {
            listView1.Items.Clear();
            imageList.Images.Clear();

            foreach (Component com in comList)
            {
                listViewAdd(com);
            }  
        }

        //向listview添加元件
        public void Add(Component com)
        {
            comList.Add(com);
            listViewAdd(com);
        }
        //listview删除元件
        private void listView_Del(int index)
        {
            imageList.Images.RemoveAt(index);
            listView1.Items.RemoveAt(index);
            comList.RemoveAt(index);

            refreshList();
        }

        private void listViewAdd(Component com)
        {
            listView1.Items.Add(com.Name);
            imageList.Images.Add(com.Image);
            listView1.Items[listView1.Items.Count - 1].Tag = com;      //每个列表绑定一个元件
            listView1.Items[listView1.Items.Count - 1].ImageIndex = listView1.Items.Count - 1;
        }

        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ListViewItem lvi = listView1.GetItemAt(e.X, e.Y);
                if (lvi != null)
                    this.listviewMenuStrip.Show(listView1, e.Location);
            }
        }

        //listView开始拖拽
        private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                DoDragDrop((Component)listView1.SelectedItems[0].Tag, DragDropEffects.Copy);
            }
        }

        //ListView右键菜单 ,删除元件
        private void 删除toolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Delete(listView1.SelectedItems[0].Index);
            listView_Del(listView1.SelectedItems[0].Index);
        }

        private void 元件信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        //beta,清空listView
        public void Clear()
        {
            listView1.Items.Clear();
            imageList.Images.Clear();
        }
    }
}
